package com.sdy.healthcode.biz.service.impl;

import com.sdy.healthcode.biz.model.SysParam;
import com.sdy.healthcode.biz.mapper.SysParamMapper;
import com.sdy.healthcode.biz.service.SysParamService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-03-09
 */
@Slf4j
@Service
public class SysParamServiceImpl extends BaseServiceImpl<SysParam> implements SysParamService {
    @Autowired
    private SysParamMapper sysParamMapper;

    @Override
    public String getOrDefault(String key, String defaultValue) {
        SysParam param = lambdaQuery().eq(SysParam::getPkey, key).one();
        return param == null ? defaultValue : param.getPvalue();
    }

    @Override
    public void updateParam(String key, String value) {
        SysParam param = lambdaQuery().eq(SysParam::getPkey, key).select(SysParam::getId).last("limit 1").one();
        if (param == null) {
            save(new SysParam().setPkey(key).setPvalue(value));
        } else {
            lambdaUpdate().eq(SysParam::getId, param.getId()).set(SysParam::getPvalue, value).update();
        }
    }
}
